package com.syh.jn.scm.infrastructure.repository.impl.upstream.base;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.upstream.entity.base.UpGoods;
import com.syh.jn.scm.domain.upstream.repository.base.UpGoodsRepository;
import com.syh.jn.scm.dto.upstream.base.UpGoodsVo;
import com.syh.jn.scm.infrastructure.repository.model.base.BusinessEntityExtGoodsInfoModel;
import com.syh.jn.scm.infrastructure.repository.model.base.goods.GoodsModel;
import com.syh.jn.scm.infrastructure.repository.model.base.goods.NewGoodsRequestModel;
import com.syh.jn.scm.infrastructure.repository.model.base.upstream.UpstreamPriceTmplModel;
import com.syh.jn.scm.infrastructure.repository.utils.MybatisTemplate;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UpGoodsRepositoryImpl extends AbstractAggregateRepository<UpGoods, GoodsModel, Long> implements UpGoodsRepository {
  @Override
  protected SQL getViewSql() {
    //model数组
    Class[] classes = {
            GoodsModel.class,
            UpstreamPriceTmplModel.class,
            BusinessEntityExtGoodsInfoModel.class
    };
    //别名数组
    String[] aliases = {
            "goods",
            "offer",
            "code"
    };

    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(classes, aliases, null),
              "offer.ID AS offerId,offer.STATUS AS upGoodsStatus,code.ID AS extCodeId,brand.NAME AS goodsBrandName,manufacturer.NAME AS manufacturerName,origin.NAME AS originName"
      );
      FROM("T_BASE_GOODS goods");
      LEFT_OUTER_JOIN(
              "T_BASE_UPSTREAM_PRICE_TMPL offer on offer.GOODS_ID = goods.ID",
              "(select * from T_BASE_BUSINESS_ENTITY_EXT_GOODS_INFO goodsInfo where goodsInfo.TERMINAL_TYPE = 'UPSTREAM') code on code.GOODS_ID = offer.GOODS_ID",
              "T_BASE_GOODS_BRAND brand on brand.ID = goods.GOODS_BRAND_ID",
              "T_BASE_MANUFACTURER manufacturer on manufacturer.ID = goods.MANUFACTURER_ID",
              "T_BASE_ORIGIN origin on origin.ID = goods.ORIGIN_ID"
      );
    }};
  }

  @Override
  protected Class getViewObject() {
    return UpGoodsVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    HashMap<String, String> weMap = new HashMap<>();
    weMap.put("id", "goods.ID");
    weMap.put("name", "goods.NAME");
    weMap.put("categoryId", "goods.CATEGORY_ID");
    weMap.put("manufacturerId", "goods.MANUFACTURER_ID");
    weMap.put("originId", "goods.ORIGIN_ID");
    weMap.put("goodsBrandId", "goods.GOODS_BRAND_ID");
    weMap.put("status", "offer.STATUS");
    weMap.put("businessEntityId", "offer.BUSINESS_ENTITY_ID");
    weMap.put("terminalType", "code.TERMINAL_TYPE");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return weMap.get(propertyName);
    });
    return parse;
  }
}
